package com.itheima.ssm.service.impl;

import com.itheima.ssm.dao.IRoleDao;
import com.itheima.ssm.domain.Permission;
import com.itheima.ssm.domain.Role;
import com.itheima.ssm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private IRoleDao iRoleDao;

    @Override
    public List<Role> findAll() throws Exception {
        return iRoleDao.findAll();
    }

    @Override
    public void save(Role role) throws Exception {
        iRoleDao.save(role);
    }

    @Override
    public Role findById(String roleId) throws Exception {
        return iRoleDao.findById(roleId);
    }

    //    删除角色【role 包含两个中间表】
    @Override
    public void delRoleById(String roleId) throws Exception {
//        从users_role表中删除
        iRoleDao.delFromUser_roleByRoleId(roleId);
//        从role_permission表中删除
        iRoleDao.delFromUser_permissinonByRoleId(roleId);
//        从role表中删除
        iRoleDao.delFromRoleByRoleId(roleId);

    }

    //根据roleId查询出该角色没有的所有权限
    @Override
    public List<Permission> findRoleByIdAndAllPermission(String roleId) throws Exception {
        return iRoleDao.findRoleByIdAndAllPermission(roleId);
    }

    @Override
    public void addPermissionToRole(String roleId, String[] permissions) throws Exception {
        for (String permission : permissions) {
            iRoleDao.addPermissionToRole(roleId, permission);
        }
    }
}
